/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.RowFilter;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.sort.RowFilters;

/**
 *
 * @author Administrator
 */
public class BaseTabController {

    protected JLabel pagesLabel;
    protected JXTable table;
    protected ArrayList<JComboBox> filterComboBoxList = new ArrayList<JComboBox>();
    protected ArrayList<Integer> filterColumnsList = new ArrayList<Integer>();
    protected ArrayList<String> filterVariableList = new ArrayList<String>();
    protected JXSearchField searchField;
    protected int searchColumn;
    protected JFormattedTextField resultsPerPageField;
    protected int[] searchColumns;
    protected String searchFieldPromptText;
    protected JButton searchButton;
    protected JButton nextButton;
    protected JButton prevButton;
    protected HashMap<Integer, String> columns = new HashMap<Integer, String>();

    public JXTable getTable() {
        return table;
    }

    public void setTable(JXTable table) {
        this.table = table;
    }

    public JXSearchField getSearchField() {
        return searchField;
    }

    public void addColumn(Integer columnNumber, String columnName) {
        columns.put(columnNumber, columnName);
    }

    public void addFilter(JComboBox filterComboBox, int filterColumns, String filterVariableName) {
        filterComboBoxList.add(filterComboBox);
        filterColumnsList.add(filterColumns);
        filterVariableList.add(filterVariableName);
    }

    public void setSearchField(JXSearchField searchField, int... filterColumns) {
        this.searchColumns = filterColumns;
        this.searchField = searchField;
    }

    public void prepareUserInterface() {
        searchField.setPrompt(searchFieldPromptText);

        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createSimpleStriping());
    }

    public void bindElements() {
        searchField.setCancelAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                searchField.setText("");
                filterTable();
            }
        });

        searchField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent evt) {
                filterTable();
            }
        });

        for (JComboBox jcb : filterComboBoxList) {
            if (jcb != null) {
                jcb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        filterTable();
                    }
                });
            }
        }


    }

    protected void filterTable() {
        List filters = new ArrayList();

        Pattern searchFieldPattern = Pattern.compile(searchField.getText() + ".*", Pattern.CASE_INSENSITIVE);
        filters.add(RowFilters.regexFilter(searchFieldPattern, this.searchColumns));

        for (int i = 0; i < filterComboBoxList.size(); i++) {
            JComboBox jcb = filterComboBoxList.get(i);
            if (jcb != null) {
                if (!jcb.getSelectedItem().toString().equals("All")) {
                    Pattern filterComboBoxPattern = Pattern.compile(jcb.getSelectedItem().toString() + ".*", Pattern.CASE_INSENSITIVE);
                    filters.add(RowFilters.regexFilter(filterComboBoxPattern, this.filterColumnsList.get(i)));
                }
            }
        }

        table.setRowFilter(RowFilter.andFilter(filters));
    }

    public String getSearchFieldPromptText() {
        return searchFieldPromptText;
    }

    public void setSearchFieldPromptText(String searchFieldPromptText) {
        this.searchFieldPromptText = searchFieldPromptText;
    }

    public JButton getSearchButton() {
        return searchButton;
    }

    public void setSearchButton(JButton searchButton) {
        this.searchButton = searchButton;
    }

    public HashMap<Integer, String> getColumns() {
        return columns;
    }

    public void setColumns(HashMap<Integer, String> columns) {
        this.columns = columns;
    }

    public JLabel getPagesLabel() {
        return pagesLabel;
    }

    public void setPagesLabel(JLabel pagesLabel) {
        this.pagesLabel = pagesLabel;
    }

    public JButton getNextButton() {
        return nextButton;
    }

    public void setNextButton(JButton nextButton) {
        this.nextButton = nextButton;
    }

    public JButton getPrevButton() {
        return prevButton;
    }

    public void setPrevButton(JButton prevButton) {
        this.prevButton = prevButton;
    }

    public JFormattedTextField getResultsPerPageField() {
        return resultsPerPageField;
    }

    public void setResultsPerPageField(JFormattedTextField resultsPerPageField) {
        this.resultsPerPageField = resultsPerPageField;
    }

    public int getSearchColumn() {
        return searchColumn;
    }

    public void setSearchColumn(int searchColumn) {
        this.searchColumn = searchColumn;
    }
}
